package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    public  List<Map> getSexCount(String startDate, String endDate){

        if(startDate != null && startDate.length() > 0 && endDate != null && endDate.length() > 0){

            startDate = startDate + "-01";
            endDate = endDate + "-31";
        }

        Map<String,String> map = new HashMap<>();
        map.put("startDate",startDate);
        map.put("endDate",endDate);

        List list = new ArrayList();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("name","男");
        map1.put("value",memberDao.getSexCount("1"));
        list.add(map1);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("name","女");
        map2.put("value",memberDao.getSexCount("0"));

        list.add(map2);
        return list;
    }

    @Override
    public List<Map<String, Object>> getAgeCount(String startDate, String endDate) {

        startDate = startDate + "-01";
        endDate = endDate + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("startDate",startDate);
        map.put("endDate",endDate);

        List<Map<String,Object>> list = new ArrayList<>();
        try {
            String before18 = DateUtils.getNumBeforeYear(18);
            String before30 = DateUtils.getNumBeforeYear(30);
            String before50 = DateUtils.getNumBeforeYear(50);
            //18岁以下
            Integer before18Count = memberDao.getAgeBeforeCount(before18);
            //18-30
            Map<String,String> dateMap = new HashMap<>();
            dateMap.put("before",before18);
            dateMap.put("end",before30);
            Integer between18_30Count = memberDao.getAgeBetweenCount(dateMap);
            //30-50
             dateMap = new HashMap<>();
            dateMap.put("before",before30);
            dateMap.put("end",before50);
            Integer between30_50Count = memberDao.getAgeBetweenCount(dateMap);
            //50以上
            Integer before50Count= memberDao.getAgeAfterCount(before50);

            Map<String,Object> map1 = new HashMap<>();
            map1.put("name","18以下");
            map1.put("value",before18Count);
            list.add(map1);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("name","18-30");
            map2.put("value",between18_30Count);
            list.add(map2);
            Map<String,Object> map3 = new HashMap<>();
            map3.put("name","30-50");
            map3.put("value",between30_50Count);
            list.add(map3);
            Map<String,Object> map4 = new HashMap<>();
            map4.put("name","50以上");
            map4.put("value",before50Count);
            list.add(map4);

            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }




    }


}
